package com.example.administrator.testxingpiao.ui.main.homepage;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SearchView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.administrator.testxingpiao.R;
import com.example.administrator.testxingpiao.base.BaseFragment;
import com.example.administrator.testxingpiao.bean.BanerList1;
import com.example.administrator.testxingpiao.bean.HomePageBean;
import com.example.administrator.testxingpiao.bean.ListBean;
import com.example.administrator.testxingpiao.config.ConfigUrl;
import com.example.administrator.testxingpiao.ui.city.CityActivity;
import com.example.administrator.testxingpiao.ui.perfom.ContentActivity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2016/8/4.
 */
public class HomePageFragment extends BaseFragment implements HomePageContract.View{

    @BindView(R.id.viewpager_homepage)
    ViewPager viewpagerHomepage;
    @BindView(R.id.radiobutton_perform)
    RadioButton radiobuttonPerform;
    @BindView(R.id.radiobutton_hotel)
    RadioButton radiobuttonHotel;
    @BindView(R.id.radiobutton_ticket)
    RadioButton radiobuttonTicket;
    @BindView(R.id.recyclerview_perform_homepage)
    RecyclerView recyclerviewPerformHomepage;
    @BindView(R.id.recyclerview_hotel_homepage)
    RecyclerView recyclerviewHotelHomepage;
    @BindView(R.id.recyclerview_ticket_homepage)
    RecyclerView recyclerviewTicketHomepage;
    @BindView(R.id.textview_city_main)
    TextView tvCity;
    @BindView(R.id.rl_top_city_homepage)
    RelativeLayout rlTopCityHomepage;
    @BindView(R.id.searchview_top_main)
    SearchView searchviewTopMain;

    private HomePageModel model;
    private HomePagePresenter presenter;
    private HomePageBean homePageBean1;
    public static final int SELECTED_PERFORM=0;
    public static final int SELECTED_HOTLE=1;
    public static final int SELECTED_TICKET=2;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View fragmentView = inflater.inflate(R.layout.fragment_homepage, container, false);
        ButterKnife.bind(this, fragmentView);
        EventBus.getDefault().register(this);
        initData();
        return fragmentView;
    }

    public void initData() {
        model = new HomePageModel();
        presenter = new HomePagePresenter(this, model);
        presenter.initData(initParams());
    }

    private HashMap<String, String> initParams() {
        HashMap<String, String> params = new HashMap<>();
        params.put(ConfigUrl.Key.ACT, "home");
        params.put(ConfigUrl.Key.REQ, "{\"v\":\"1.0\",\"o\":\"wap\",\"t\":1465978896117,\"an\":\"ixingmei\",\"av\":\"1.0.0\",\"ci\":\"289\",\"lu\":\"\",\"lt\":\"\",\"args\":{}}");
        return params;
    }

    @OnClick({R.id.radiobutton_perform, R.id.radiobutton_hotel, R.id.radiobutton_ticket})
    public void onClick(View view) {
        Intent intent=new Intent(getActivity(), ContentActivity.class);
        switch (view.getId()) {
            case R.id.radiobutton_perform:
                intent.putExtra("position",SELECTED_PERFORM);
                startActivity(intent);
                break;
            case R.id.radiobutton_hotel:
                intent.putExtra("position",SELECTED_HOTLE);
                startActivity(intent);
                break;
            case R.id.radiobutton_ticket:
                intent.putExtra("position",SELECTED_TICKET);
                startActivity(intent);
                break;
        }
    }

    @Override
    public void onSuccessGetHomePageBean(HomePageBean homePageBean) {
        homePageBean1 = homePageBean;
        setPagerAdapter();
        setRecyclerAdapter();

    }

    @Override
    public void onFailGetHomePageBean(String msg) {

    }

    private void setPagerAdapter() {

        List<BanerList1> banerList = homePageBean1.getResult().getBanerList1();
        TopPagerAdapter pagerAdapter = new TopPagerAdapter(banerList,getContext());
        if (banerList != null) {
            viewpagerHomepage.setAdapter(pagerAdapter);
        }
    }

    private void setRecyclerAdapter() {

        List<ListBean> listPerfrom = homePageBean1.getResult().getRecommendInfo2().getList();
        List<ListBean> listHotel = homePageBean1.getResult().getRecommendInfo3().getList();
        List<ListBean> listTicket = homePageBean1.getResult().getRecommendInfo4().getList();

        setRecyclerViewAdapter(listPerfrom, recyclerviewPerformHomepage);
        setRecyclerViewAdapter(listHotel, recyclerviewHotelHomepage);
        setRecyclerViewAdapter(listTicket, recyclerviewTicketHomepage);
    }

    private void setRecyclerViewAdapter(List<ListBean> list, RecyclerView rv) {

        LinearLayoutManager manager = new LinearLayoutManager(getContext());
        manager.setOrientation(LinearLayoutManager.HORIZONTAL);
        rv.setLayoutManager(manager);
        RecyclerAdapter adapter = new RecyclerAdapter(list, getContext());
        rv.setAdapter(adapter);
    }

    @OnClick(R.id.rl_top_city_homepage)

    public void onClick() {
        Intent intent = new Intent(getContext(), CityActivity.class);
        startActivity(intent);
    }

    //从3.0以后必须添加注解
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void receiveImg(String city) {

        tvCity.setText(city);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
